import java.awt.Component;
import java.awt.Frame;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
 *This class is main class where all the classes are called
 * @author paulsmith
 *
 */
public class GameDriver 
{
        /**
         * This is the main method which has the references of all the classes
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException
        {
                JFrame frame = new JFrame("the");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(100, 100, 500, 400);
                    
                GameView gv = new GameView();
                StatusModel sm = new StatusModel();
                StatusView sv = new StatusView(sm);
                StatusController sc = new StatusController(sv, sm);
                MainPlayerModel mm = new MainPlayerModel();
                MainPlayerView mv = new MainPlayerView(mm);
                MainPlayerController mc = new MainPlayerController(mv, mm);
                InventoryModel im = new InventoryModel();
                InventoryController ic = new InventoryController(im);
                StoreModel stm = new StoreModel();
                StoreView stv = new StoreView(stm, mm, im);
                StoreController stc = new StoreController(ic, mc, stv, stm);
                MilestoneModel milestone = new MilestoneModel();
                MapController mapCont = new MapController(sm, milestone, im, stv);
                MilestoneView msv = new MilestoneView(new ImageIcon(MainView.class.getResource("vancouver.PNG")).getImage(), milestone, mapCont);
                MainView mainView = new MainView(mapCont);
                MapView map = new MapView(new ImageIcon(MainView.class.getResource("northamerica.PNG")).getImage());
                map.setController(mapCont);
                WinView winView = new WinView();
                RandomEventGenerator rg = new RandomEventGenerator(mm, im);
                MainController mainCont = new MainController(im, mainView, sm, sv, sc, map, mapCont, winView, milestone, msv, stv, mm, rg);
                boolean isPassCodeFound = false;
                
                
                frame.getContentPane().add(sv);

                while(!gv.startNewGame())
                {
                	gv.setVisible(true);
                       
                    if(gv.loadGame())
                    {
                       	String passCode = (String)JOptionPane.showInputDialog(
                       							new JFrame("Load"), "Please enter your password", 
                       							"Customized Dialog", JOptionPane.PLAIN_MESSAGE);
                       	Scanner inFile = new Scanner(new File("saveFile.txt"));
                       	
                       	while(inFile.hasNext())
                       	{
                       		if(inFile.next().equals(passCode))
                       		{
                       			String str = inFile.nextLine();
                       			loadGameState(sm, im, mm, mapCont, stv, mainView, str);
                       			isPassCodeFound = true;
                       			break;
                       		}
                       	}
                       	inFile.close();
                       	
                       	if(isPassCodeFound)
                       		break;
                       	else
                       	{
                       		if(passCode != null)
                       		{
                       			JOptionPane.showMessageDialog(new JFrame("Sorry!"),
                                        "" + passCode + " is not a valid passphrase.");
                       		}
                       		gv.setLoadGame(false);
                       	}
                    }
                }
                gv.setVisible(false);
                
                if(!isPassCodeFound)
                {
                	while(!mc.hasUserPressedNext())
                   		mv.setVisible(true);
                    mv.setVisible(false);
                    
                    while(!sc.isUserDone())
                    	frame.setVisible(true);
                    frame.setVisible(false);
                    
                    stv.setCashText(mm.getTotalCash());
                    mainView.setVisible(true);
                    stv.setVisible(true);
                    mainView.setDistArea(sm.getDistTravelled());
                    mainView.setFoodLbs(im.getItems().get(1).getQuantity());
                    //System.out.println(im.getItems().get(1).getQuantity());
                }
                else
                	mainView.setVisible(true);
        }
        
        public static void loadGameState(StatusModel sm, InventoryModel im, MainPlayerModel mm, 
        								 MapController mpc, StoreView stv, MainView mv, 
        								 String gameState) throws IOException
        {
        	Scanner inLine = new Scanner(gameState);
        	sm.setDistTravelled(Integer.parseInt(inLine.next()));
        	sm.setPace(Integer.parseInt(inLine.next()));
        	sm.setRation(Integer.parseInt(inLine.next()));
        	
        	String quantities = "";
        	for(int i = 0; i < 7; i++)
        		quantities += inLine.next() + " ";
        		
        	im.setQuantities(quantities);
        	mm.setProfession(Integer.parseInt(inLine.next()));
        	int numPartyMems = Integer.parseInt(inLine.next());
        	
        	String[] partyMems = new String[4];
        	for(int i = 0; i < numPartyMems; i++)
        		partyMems[i] = inLine.next();
        	
        	mm.setPartyMembers(partyMems);
        	mm.setNumPartyMembers(numPartyMems);
        	mm.setTotalCash(Double.parseDouble(inLine.next()));
        	mm.setName(inLine.next());
        	mpc.setPlayerX(Double.parseDouble(inLine.next()));
        	mpc.setPlayerY(Double.parseDouble(inLine.next()));
        	mpc.setNextIndex(Integer.parseInt(inLine.next()));
        	
        	stv.setCashText(mm.getTotalCash());
        	mv.setDistArea(sm.getDistTravelled());
            mv.setFoodLbs(im.getItems().get(1).getQuantity());
        }
}
